#!/usr/bin/env python

#Copyright (C) 2008 Luis Felipe Millani (lf.millani@gmail.com)

#This file is part of MagicLyrics.

#MagicLyrics is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.

#MagicLyrics is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with MagicLyrics.  If not, see <http://www.gnu.org/licenses/>.
import wx
app=wx.App(redirect=0)
import wx.lib.newevent
from wx.lib.wordwrap import wordwrap
import wx.lib.buttons as buttons
import os
import thread
import icons

class MainPanel(wx.Panel):
	def manual_search(self,event):
		song=self.song.GetValue()
		if '-' in song:
			song=(song[:song.find('-')].strip(),song[song.find('-')+1:].strip())
		else:
			song=('',song.strip())
		if song==self.playing():
			force=True
		else:
			force=False
		thread.start_new_thread(self.parent.get_lyrics,tuple(),
			{'song':song,
			'status_report':lambda s:wx.PostEvent(self.parent,self.parent.statusUpdateEvent(status=s)),
			'force':force,
			'do_after':lambda l:wx.PostEvent(self.parent,self.parent.gotLyricsEvent(lyrics=l))})
	def __init__(self,parent,id,get_playing):
		wx.Panel.__init__(self,parent,wx.ID_ANY)
		self.parent = parent
		self.playing = get_playing
		self.song = wx.TextCtrl(self, -1,style=wx.TE_PROCESS_ENTER)
		self.lyrics = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)
		self.aboutButton = buttons.GenBitmapButton(self, -1, icons.bitmap('about'),style=wx.NO_BORDER)
		self.aboutButton.SetInitialSize((20,20))
		
		if parent.get_playing()==None:
			self.set_song(('artist','song [enter]'))
			self.set_lyrics('currently supported players:\n\nlinux: Banshee, Rhythmbox\n\nwindows: musikCube')
		
		
		self.Bind(wx.EVT_TEXT_ENTER, self.manual_search, self.song)
		self.Bind(wx.EVT_BUTTON,self.about,self.aboutButton)
		
		
		# sizers
		self.sizer=wx.BoxSizer(wx.VERTICAL)
		top=wx.BoxSizer(wx.HORIZONTAL)
		top.Add(self.song,1,wx.ALIGN_LEFT)
		top.Add(self.aboutButton,0,wx.ALIGN_RIGHT)
		self.sizer.Add(top,0,wx.EXPAND)
		self.sizer.Add(self.lyrics,1,wx.EXPAND)
		self.SetSizer(self.sizer)
		self.SetAutoLayout(1)
		self.sizer.Fit(self)
	
	def set_lyrics(self,lyrics):
		self.lyrics.SetValue(lyrics)
	def set_song(self,song):
		self.song.SetValue(song[0]+' - '+song[1])
	
	def about(self, evt):
		description = wordwrap(
			'MagicLyrics automatically finds and displays the '
			'lyrics for the currently playing song',
			350,wx.ClientDC(self))
		
		licence = wordwrap(
			'MagicLyrics is free software: you can redistribute it and/or modify '
			'it under the terms of the GNU General Public License as published by '
			'the Free Software Foundation, either version 3 of the License, or '
			'(at your option) any later version.\n'
			'\n'
			'MagicLyrics is distributed in the hope that it will be useful, '
			'but WITHOUT ANY WARRANTY; without even the implied warranty of '
			'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the '
			'GNU General Public License for more details.\n'
			'\n'
			'You should have received a copy of the GNU General Public License '
			'along with MagicLyrics.  If not, see <http://www.gnu.org/licenses/>.',
			350,wx.ClientDC(self))
		
		info = wx.AboutDialogInfo()
		
		info.SetIcon(icons.icon('main'))
		info.SetName('MagicLyrics')
		info.SetVersion('1.0')
		info.SetDescription(description)
		info.SetCopyright('Copyright 2008 Luis Felipe Millani')
		info.SetWebSite('http://code.google.com/p/magiclyrics')
		info.SetLicence(licence)
		info.SetDevelopers(['Luis Felipe Millani (lf.millani@gmail.com)'])
		
		wx.AboutBox(info)
class MainWindow(wx.Frame):
	def __init__(self,get_playing,get_lyrics):
		x=300
		if wx.SystemSettings.GetMetric(wx.SYS_SCREEN_Y)>650+30:
			y=650
		else:
			y=wx.SystemSettings.GetMetric(wx.SYS_SCREEN_Y)-30
		wx.Frame.__init__(self,None,-1,'MagicLyrics',
						(wx.SystemSettings.GetMetric(wx.SYS_SCREEN_X)-x,0),(x,y))
		
		self.get_playing=get_playing
		self.get_lyrics=get_lyrics
		self.panel=MainPanel(self, -1,get_playing)
		self.current_song=None
		
		self.t=wx.Timer(self)
		self.t.Start(1000)
		self.Bind(wx.EVT_TIMER,self.check_song)
		
		self.Bind(wx.EVT_CLOSE,self.OnExit)
		
		self.gotLyricsEvent,EVT_GOT_LYRICS=wx.lib.newevent.NewEvent()
		self.Bind(EVT_GOT_LYRICS,self.got_lyrics)
		
		self.statusUpdateEvent,EVT_STATUS_UPD=wx.lib.newevent.NewEvent()
		self.Bind(EVT_STATUS_UPD,self.status_update)
		
		self.SetIcon(icons.icon('main'))
		
		self.Show(True)
	def status_update(self,evt):
		t='MagicLyrics'
		if evt.status!='':t+=' - '+evt.status
		self.SetTitle(t)
	def got_lyrics(self,evt):
		self.panel.set_lyrics(evt.lyrics)
	def check_song(self,evt):
		current_song=self.get_playing()
		if not current_song in (self.current_song,(u'',u'')):
			self.current_song=current_song
			self.panel.set_song(current_song)
			self.panel.set_lyrics(u'')
			thread.start_new_thread(self.get_lyrics,tuple(),
				{'song':current_song,
				'status_report':lambda s:wx.PostEvent(self,self.statusUpdateEvent(status=s)),
				'force':False,
				'do_after':lambda l:wx.PostEvent(self,self.gotLyricsEvent(lyrics=l))})
	def OnExit(self,e):
		self.Destroy()

def update_lyrics(song,frame,lyrics_ctrl,force=False):
	def update_status_in_title(status=''):
		s='MagicLyrics'
		if status!='':s+=' - '+status
		frame.SetTitle(s)
	lyrics=get_lyrics(song,status_report=update_status_in_title,force=force)
	update_status_in_title('')
	if lyrics==None: # may be unnecessary
		lyrics=''
	lyrics_ctrl.SetValue(lyrics)


def start(get_playing,get_lyrics_function):
	global get_lyrics # TODO fix this later
	get_lyrics=get_lyrics_function
	del get_lyrics_function
	frame = MainWindow(get_playing,get_lyrics)
	app.MainLoop()